package com.project.persistence.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.project.bean.Features;
import com.project.bean.Function;
import com.project.persistence.db.Connect;

public class FeaturesDAOImpl extends AbstractDAO<Features> {

	@Override
	public List<Features> getAll() {
		Connect connect = new Connect();
		List<Features> features = new ArrayList<Features>();
		try {
			
			Connection conn = connect.getConnection();
			Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(getFeaturesQuery());
			
            while(rs.next()) {
            	Features feature = new Features();
            	
            	feature.setFeatureId(rs.getInt("id"));
            	feature.setFeatureName(rs.getString("name"));
            	feature.setFeatureDescription(rs.getString("description"));
            	feature.setFeatureLink(rs.getString("link"));
            	feature.setActive(rs.getBoolean("active"));
            	List<Function> list = new ArrayList<Function>();
            	
            	for(String func : Arrays.asList(rs.getString("functions").split(","))) {
            		Function function = new Function();
            		function.setName(func);
            		list.add(function);
            	}
            	
            	feature.setFunctions(list);
            	
            	features.add(feature);
			}
			
		} catch (Exception ex) {
			//TODO acrescentar error no log
			ex.printStackTrace();
		} finally {
			connect.closeConnection();
		}
		
		return features;
	}

	@Override
	public List<Features> getAllById(Integer id) {
		Connect connect = new Connect();
		List<Features> features = new ArrayList<Features>();
		try {
			
			Connection conn = connect.getConnection();
			PreparedStatement pstm = conn.prepareStatement(getFeaturesByIdQuery());
            pstm.setInt(1, id);
            ResultSet rs = pstm.executeQuery();
			
            while(rs.next()) {
            	Features feature = new Features();	
            	
            	feature.setFeatureId(rs.getInt("id"));
            	feature.setFeatureName(rs.getString("name"));
            	feature.setFeatureDescription(rs.getString("description"));
            	feature.setFeatureLink(rs.getString("link"));
            	feature.setActive(rs.getBoolean("active"));
            	
            	List<Function> list = new ArrayList<Function>();
            	
            	for(String func : Arrays.asList(rs.getString("functions").split(","))) {
            		Function function = new Function();
            		function.setName(func);
            		list.add(function);
            	}
            	
            	feature.setFunctions(list);
            	features.add(feature);
			}
			
		} catch (Exception ex) {
			//TODO acrescentar error no log
			ex.printStackTrace();
		} finally {
			connect.closeConnection();
		}
		
		return features;
	}
	
	@Override
	public Boolean insertObject(Features f, Connection conn) throws SQLException {
		
		PreparedStatement pstm = conn.prepareStatement(getInsertQuery());

		pstm.setString(1, f.getFeatureName());
		pstm.setString(2, f.getFeatureDescription());
		pstm.setString(3, f.getFeatureLink());
		pstm.setString(4, f.getFunctionToString());

		return !pstm.execute();
		
	}

	@Override
	public Boolean updateObject(Features f, Connection conn) throws SQLException {
		PreparedStatement pstm = conn.prepareStatement(getUpdateQuery());

		pstm.setString(1, f.getFeatureName());
		pstm.setString(2, f.getFeatureDescription());
		pstm.setString(3, f.getFeatureLink());
		pstm.setString(4, f.getFunctionToString());
		pstm.setInt(5, f.getFeatureId());

		return !pstm.execute();
	}

	@Override
	public Boolean disableObject(Features f, Connection conn) throws SQLException {
		
		PreparedStatement pstm = conn.prepareStatement(getEnableDisableQuery());

		pstm.setInt(1, 0);
		pstm.setInt(2, f.getFeatureId());

		return !pstm.execute();
	}
	
	@Override
	public Boolean enableObject(Features f, Connection conn) throws SQLException {
		PreparedStatement pstm = conn.prepareStatement(getEnableDisableQuery());

		pstm.setInt(1, 1);
		pstm.setInt(2, f.getFeatureId());

		return !pstm.execute();
	}
	
	public List<String> getFeaturesFunctions(String link) {
		Connect connect = new Connect();
		List<String> functions = new ArrayList<String>();
		try {
			
			Connection conn = connect.getConnection();
			PreparedStatement pstm = conn.prepareStatement(getFunctionsFromFeatureURLQuery());
            pstm.setString(1, link);
            ResultSet rs = pstm.executeQuery();
			
            while(rs.next()) {
            	functions = Arrays.asList(rs.getString("functions").split(";"));
			}
			
		} catch (Exception ex) {
			//TODO acrescentar error no log
			ex.printStackTrace();
		} finally {
			connect.closeConnection();
		}
		
		return functions;
	}
	
	@Override
	public Features getObject(Features t) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Features parseObjectFromRs(ResultSet rs) {
		// TODO Auto-generated method stub
		return null;
	}
	
	private String getFeaturesQuery() {
		return "SELECT f.id, f.name, f.description, f.link, f.functions, f.active "
				+ " FROM features f ";
	}
	
	private String getFeaturesByIdQuery() {
		return "SELECT  f.id, f.name, f.description, f.link, f.functions, f.active "
				+ " FROM features f, roles_features rf "
				+ " WHERE f.id = rf.id_feature"
				+ " AND rf.id_role = ?"
				+ " AND f.active = 1";
	}
	
	private String getInsertQuery() {
		return "INSERT INTO features (name, description, link, functions, active) VALUES (?,?,?,?,1);";
	}
	
	private String getUpdateQuery() {
		return "UPDATE features SET name = ?, description = ?, link = ?, functions= ?, active = 1 WHERE id = ?";
	}
	
	private String getEnableDisableQuery() {
		return "UPDATE features SET active = ? WHERE id = ?";
	}
	
	private String getFunctionsFromFeatureURLQuery() {
		return "SELECT functions FROM features WHERE link = ?";
	}	
}
